package api

import (
	"a_plus_station/global"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
)

func GetChaptcha(ctx *gin.Context) {

	id, base64, _, err := global.CP.Generate()
	if err != nil {
		zap.S().Info("生成验证码失败", err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": "生成图片验证码失败",
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"captcha":    base64,
		"captcha_id": id,
	})
}

func VerifyChaptCha(ctx *gin.Context) {
	answer := ctx.PostForm("answer")
	captchaId := ctx.PostForm("captchaId")

	result := global.CP.Verify(captchaId, answer, true)

	ctx.JSON(http.StatusOK, gin.H{
		"answer":     answer,
		"captcha_id": captchaId,
		"result":     result,
	})
}
